{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "from tensorflow.keras.utils import to_categorical\n",
    "import jieba\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import os\n",
    "from gensim.models import Word2Vec\n",
    "import json\n",
    "from tensorflow.keras.layers import (Bidirectional,\n",
    "                                     Embedding,\n",
    "                                     GRU, \n",
    "                                     GlobalAveragePooling1D,\n",
    "                                     GlobalMaxPooling1D,\n",
    "                                     Concatenate,\n",
    "                                     SpatialDropout1D,\n",
    "                                     BatchNormalization,\n",
    "                                     Dropout,\n",
    "                                     Dense,\n",
    "                                     Activation,\n",
    "                                     concatenate,\n",
    "                                     Input\n",
    "                                    )                        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "df_train = pd.read_csv('./data/nCoV_100k_train.labled.csv',engine ='python',nrows=99900)\n",
    "df_test = pd.read_csv('./data/nCov_10k_test.csv',engine ='python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(99900, 7)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def cut_words(sentence):\n",
    "    #print sentence\n",
    "    return \" \".join(jieba.cut(sentence))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "MAX_SEQUENCE_LENGTH = 100\n",
    "input_categories = '微博中文内容'\n",
    "output_categories = '情感倾向'\n",
    "df_train = df_train[df_train[output_categories].isin(['-1','0','1'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\37473\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.713 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    }
   ],
   "source": [
    "df_train['sentence_cuted'] =  df_train[input_categories].astype(str).apply(cut_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "df_test['sentence_cuted'] =  df_test[input_categories].astype(str).apply(cut_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df_train.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>微博id</th>\n",
       "      <th>微博发布时间</th>\n",
       "      <th>发布人账号</th>\n",
       "      <th>微博中文内容</th>\n",
       "      <th>微博图片</th>\n",
       "      <th>微博视频</th>\n",
       "      <th>情感倾向</th>\n",
       "      <th>sentence_cuted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4456072029125500</td>\n",
       "      <td>01月01日 23:50</td>\n",
       "      <td>存曦1988</td>\n",
       "      <td>写在年末冬初孩子流感的第五天，我们仍然没有忘记热情拥抱这2020年的第一天。带着一丝迷信，早...</td>\n",
       "      <td>['https://ww2.sinaimg.cn/orj360/005VnA1zly1gah...</td>\n",
       "      <td>[]</td>\n",
       "      <td>0</td>\n",
       "      <td>写 在 年末 冬初 孩子 流感 的 第五天 ， 我们 仍然 没有 忘记 热情 拥抱 这 20...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4456074167480980</td>\n",
       "      <td>01月01日 23:58</td>\n",
       "      <td>LunaKrys</td>\n",
       "      <td>开年大模型…累到以为自己发烧了腰疼膝盖疼腿疼胳膊疼脖子疼#Luna的Krystallife#?</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "      <td>-1</td>\n",
       "      <td>开年 大 模型 … 累到 以为 自己 发烧 了 腰疼 膝盖 疼 腿疼 胳膊 疼 脖子 疼 #...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4456054253264520</td>\n",
       "      <td>01月01日 22:39</td>\n",
       "      <td>小王爷学辩论o_O</td>\n",
       "      <td>�偳癯空饩褪俏业�，爹，发烧快好，毕竟美好的假期拿来养病不太好，假期还是要好好享受快乐，爹，...</td>\n",
       "      <td>['https://ww2.sinaimg.cn/thumb150/006ymYXKgy1g...</td>\n",
       "      <td>[]</td>\n",
       "      <td>1</td>\n",
       "      <td>� 偳 癯 空 饩 褪 俏 业 � ， 爹 ， 发烧 快 好 ， 毕竟 美好 的 假期 拿来...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4456061509126470</td>\n",
       "      <td>01月01日 23:08</td>\n",
       "      <td>芩鎟</td>\n",
       "      <td>新年的第一天感冒又发烧的也太衰了但是我要想着明天一定会好的?</td>\n",
       "      <td>['https://ww2.sinaimg.cn/orj360/005FL9LZgy1gah...</td>\n",
       "      <td>[]</td>\n",
       "      <td>1</td>\n",
       "      <td>新年 的 第一天 感冒 又 发烧 的 也 太衰 了 但是 我要 想着 明天 一定 会 好 的 ?</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4455979322528190</td>\n",
       "      <td>01月01日 17:42</td>\n",
       "      <td>changlwj</td>\n",
       "      <td>问：我们意念里有坏的想法了，天神就会给记下来，那如果有好的想法也会被记下来吗？答：那当然了。...</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "      <td>1</td>\n",
       "      <td>问 ： 我们 意念 里 有 坏 的 想法 了 ， 天神 就 会 给 记下来 ， 那 如果 有...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               微博id        微博发布时间      发布人账号  \\\n",
       "0  4456072029125500  01月01日 23:50     存曦1988   \n",
       "1  4456074167480980  01月01日 23:58   LunaKrys   \n",
       "2  4456054253264520  01月01日 22:39  小王爷学辩论o_O   \n",
       "3  4456061509126470  01月01日 23:08         芩鎟   \n",
       "4  4455979322528190  01月01日 17:42   changlwj   \n",
       "\n",
       "                                              微博中文内容  \\\n",
       "0  写在年末冬初孩子流感的第五天，我们仍然没有忘记热情拥抱这2020年的第一天。带着一丝迷信，早...   \n",
       "1    开年大模型…累到以为自己发烧了腰疼膝盖疼腿疼胳膊疼脖子疼#Luna的Krystallife#?   \n",
       "2  �偳癯空饩褪俏业�，爹，发烧快好，毕竟美好的假期拿来养病不太好，假期还是要好好享受快乐，爹，...   \n",
       "3                     新年的第一天感冒又发烧的也太衰了但是我要想着明天一定会好的?   \n",
       "4  问：我们意念里有坏的想法了，天神就会给记下来，那如果有好的想法也会被记下来吗？答：那当然了。...   \n",
       "\n",
       "                                                微博图片 微博视频 情感倾向  \\\n",
       "0  ['https://ww2.sinaimg.cn/orj360/005VnA1zly1gah...   []    0   \n",
       "1                                                 []   []   -1   \n",
       "2  ['https://ww2.sinaimg.cn/thumb150/006ymYXKgy1g...   []    1   \n",
       "3  ['https://ww2.sinaimg.cn/orj360/005FL9LZgy1gah...   []    1   \n",
       "4                                                 []   []    1   \n",
       "\n",
       "                                      sentence_cuted  \n",
       "0  写 在 年末 冬初 孩子 流感 的 第五天 ， 我们 仍然 没有 忘记 热情 拥抱 这 20...  \n",
       "1  开年 大 模型 … 累到 以为 自己 发烧 了 腰疼 膝盖 疼 腿疼 胳膊 疼 脖子 疼 #...  \n",
       "2  � 偳 癯 空 饩 褪 俏 业 � ， 爹 ， 发烧 快 好 ， 毕竟 美好 的 假期 拿来...  \n",
       "3   新年 的 第一天 感冒 又 发烧 的 也 太衰 了 但是 我要 想着 明天 一定 会 好 的 ?  \n",
       "4  问 ： 我们 意念 里 有 坏 的 想法 了 ， 天神 就 会 给 记下来 ， 那 如果 有...  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>微博id</th>\n",
       "      <th>微博发布时间</th>\n",
       "      <th>发布人账号</th>\n",
       "      <th>微博中文内容</th>\n",
       "      <th>微博图片</th>\n",
       "      <th>微博视频</th>\n",
       "      <th>sentence_cuted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4456068992182160</td>\n",
       "      <td>01月01日 23:38</td>\n",
       "      <td>-精緻的豬豬女戰士-</td>\n",
       "      <td>#你好2020#新年第一天元气满满的早起出门买早饭结果高估了自己抗冻能力回家成功冻发烧（大概...</td>\n",
       "      <td>['https://ww2.sinaimg.cn/thumb150/745aa591ly1g...</td>\n",
       "      <td>[]</td>\n",
       "      <td># 你好 2020 # 新年 第一天 元气 满满的 早起 出门 买 早饭 结果 高估 了 自...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4456424178427250</td>\n",
       "      <td>01月02日 23:09</td>\n",
       "      <td>liujunyi88</td>\n",
       "      <td>大宝又感冒鼻塞咳嗽了，还有发烧。队友加班几天不回。感觉自己的情绪在家已然是随时引爆的状态。情...</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "      <td>大宝 又 感冒 鼻塞 咳嗽 了 ， 还有 发烧 。 队友 加班 几天 不回 。 感觉 自己 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4456797466940200</td>\n",
       "      <td>01月03日 23:53</td>\n",
       "      <td>ablsa</td>\n",
       "      <td>还要去输两天液，这天也太容易感冒发烧了，一定要多喝热水啊?</td>\n",
       "      <td>['https://ww3.sinaimg.cn/orj360/006fTidCly1gaj...</td>\n",
       "      <td>[]</td>\n",
       "      <td>还要 去 输 两天 液 ， 这天 也 太 容易 感冒 发烧 了 ， 一定 要 多 喝 热水 啊 ?</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4456791021108920</td>\n",
       "      <td>01月03日 23:27</td>\n",
       "      <td>喵吃鱼干Lynn</td>\n",
       "      <td>我太难了别人怎么发烧都没事就我一检查甲型流感?</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "      <td>我太难 了 别人 怎么 发烧 都 没事 就 我 一 检查 甲型 流感 ?</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4457086404997440</td>\n",
       "      <td>01月04日 19:01</td>\n",
       "      <td>我的发小今年必脱单</td>\n",
       "      <td>果然是要病一场的喽回来第三天开始感冒今儿还发烧了喉咙眼睛都难受的一匹怎么样能不经意让我的毕设...</td>\n",
       "      <td>[]</td>\n",
       "      <td>[]</td>\n",
       "      <td>果然 是 要 病 一场 的 喽 回来 第三天 开始 感冒 今儿 还 发烧 了 喉咙 眼睛 都...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               微博id        微博发布时间       发布人账号  \\\n",
       "0  4456068992182160  01月01日 23:38  -精緻的豬豬女戰士-   \n",
       "1  4456424178427250  01月02日 23:09  liujunyi88   \n",
       "2  4456797466940200  01月03日 23:53       ablsa   \n",
       "3  4456791021108920  01月03日 23:27    喵吃鱼干Lynn   \n",
       "4  4457086404997440  01月04日 19:01   我的发小今年必脱单   \n",
       "\n",
       "                                              微博中文内容  \\\n",
       "0  #你好2020#新年第一天元气满满的早起出门买早饭结果高估了自己抗冻能力回家成功冻发烧（大概...   \n",
       "1  大宝又感冒鼻塞咳嗽了，还有发烧。队友加班几天不回。感觉自己的情绪在家已然是随时引爆的状态。情...   \n",
       "2                      还要去输两天液，这天也太容易感冒发烧了，一定要多喝热水啊?   \n",
       "3                            我太难了别人怎么发烧都没事就我一检查甲型流感?   \n",
       "4  果然是要病一场的喽回来第三天开始感冒今儿还发烧了喉咙眼睛都难受的一匹怎么样能不经意让我的毕设...   \n",
       "\n",
       "                                                微博图片 微博视频  \\\n",
       "0  ['https://ww2.sinaimg.cn/thumb150/745aa591ly1g...   []   \n",
       "1                                                 []   []   \n",
       "2  ['https://ww3.sinaimg.cn/orj360/006fTidCly1gaj...   []   \n",
       "3                                                 []   []   \n",
       "4                                                 []   []   \n",
       "\n",
       "                                      sentence_cuted  \n",
       "0  # 你好 2020 # 新年 第一天 元气 满满的 早起 出门 买 早饭 结果 高估 了 自...  \n",
       "1  大宝 又 感冒 鼻塞 咳嗽 了 ， 还有 发烧 。 队友 加班 几天 不回 。 感觉 自己 ...  \n",
       "2  还要 去 输 两天 液 ， 这天 也 太 容易 感冒 发烧 了 ， 一定 要 多 喝 热水 啊 ?  \n",
       "3               我太难 了 别人 怎么 发烧 都 没事 就 我 一 检查 甲型 流感 ?  \n",
       "4  果然 是 要 病 一场 的 喽 回来 第三天 开始 感冒 今儿 还 发烧 了 喉咙 眼睛 都...  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def compute_output_arrays(df, columns):\n",
    "    return np.asarray(df[columns].astype(int) + 1)\n",
    "outputs = compute_output_arrays(df_train, output_categories)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 2, ..., 2, 1, 1])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "train = df_train\n",
    "test = df_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=50000, \n",
    "                                                  lower=False,filters=\"\")# 对数据构造词典\n",
    "tokenizer.fit_on_texts(train['sentence_cuted'].tolist()+test['sentence_cuted'].tolist())\n",
    "\n",
    "json_str = tokenizer.to_json()\n",
    "json.dump(json_str,open(r'tokenizer_config.json','w'))#文件路径前面加上 r 是为了避免转义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "train_ = tokenizer.texts_to_sequences(train['sentence_cuted'].values)# 文本 ---> 数字\n",
    "test_ = tokenizer.texts_to_sequences(test['sentence_cuted'].values)# 同上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "maxlen = int(np.percentile(list(map(lambda x: len(x),train_)),95))# 统计95% 字的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "maxlen = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "train_ = tf.keras.preprocessing.sequence.pad_sequences(train_, maxlen=maxlen,\n",
    "                                                      padding='pre',truncating='pre',value=0.0)\n",
    "test_ = tf.keras.preprocessing.sequence.pad_sequences(test_, maxlen=maxlen,\n",
    "                                                     padding='pre',truncating='pre',value=0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "word_vocab = tokenizer.word_index\n",
    "print(len(word_vocab))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_val,y_train,y_val = train_test_split(train_,outputs,\n",
    "                                               test_size=0.1,random_state=666)# 0.1 或 0.05 ,因为数据量比较大，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(10000).batch(64)# 打乱,随机取batch训练\n",
    "val_ds = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(64)# 测试集批处理的大小可以和训练机匹配的大小不一样?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "from gensim.models import Word2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "all_data=pd.concat([train['sentence_cuted'],test['sentence_cuted']])\n",
    "file_name = './word2vec/Word2Vec_word_200.model'\n",
    "if not os.path.exists(file_name):\n",
    "    print('training')\n",
    "    model_word2vec = Word2Vec([document.split(' ')for document in all_data.values],\n",
    "                     size=200, \n",
    "                     window=5,\n",
    "                     iter=10, \n",
    "                     workers=11, \n",
    "                     seed=2018, \n",
    "                     min_count=2)\n",
    "\n",
    "    model_word2vec.save(file_name)\n",
    "else:\n",
    "    print('loading')\n",
    "    model_word2vec = Word2Vec.load(file_name)\n",
    "print(\"add word2vec finished....\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "model_word2vec.wv.vocab.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "model_word2vec.most_similar(\"上海\",topn=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "count = 0\n",
    "word_vocal_reverse = {}\n",
    "embedding_matrix = np.zeros((len(word_vocab) + 1, 200)) # 为什么加1 ? 因为字典没有0, 我们为补全造了一个0\n",
    "for word, i in word_vocab.items():\n",
    "    word_vocal_reverse[i]=word\n",
    "    embedding_vector = model_word2vec.wv[word] if word in model_word2vec else None # 取出向量\n",
    "    if embedding_vector is not None:\n",
    "        count += 1\n",
    "        embedding_matrix[i] = embedding_vector# i 表示词典中的位置, 通过embedding,来代替 字的向量表示\n",
    "    else:\n",
    "        unk_vec = np.random.random(200) * 0.5\n",
    "        unk_vec = unk_vec - unk_vec.mean()# 减去均值\n",
    "        embedding_matrix[i] = unk_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "np.save('idsMatrix', embedding_matrix)\n",
    "\n",
    "word_vocal_reverse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "def build_model(sent_length, embeddings_weight,class_num):\n",
    "    content = Input(shape=(sent_length,), dtype='int32')\n",
    "    embedding = Embedding(\n",
    "        name=\"sentence_cuted\",\n",
    "        input_dim=embeddings_weight.shape[0],\n",
    "        weights=[embeddings_weight],\n",
    "        output_dim=embeddings_weight.shape[1],\n",
    "        trainable=False)\n",
    "\n",
    "    x = SpatialDropout1D(0.2)(embedding(content))\n",
    "\n",
    "    x = Bidirectional(GRU(200, return_sequences=True))(x)\n",
    "#     x = Bidirectional(GRU(200, return_sequences=True))(x)\n",
    "\n",
    "    avg_pool = GlobalAveragePooling1D()(x)# 全军平均池化\n",
    "    max_pool = GlobalMaxPooling1D()(x)# 全局最大池化\n",
    "\n",
    "    conc = concatenate([avg_pool, max_pool]) # 特征放到一起，\n",
    "    \n",
    "    x = Dense(1000)(conc)# 全连接层\n",
    "    x = BatchNormalization()(x)\n",
    "    x = Activation(activation=\"relu\")(x)\n",
    "    x = Dropout(0.2)(x)\n",
    "    x = Dense(500)(x)\n",
    "    x = BatchNormalization()(x)\n",
    "    x = Activation(activation=\"relu\")(x)\n",
    "    output = Dense(class_num, activation=\"softmax\")(x)\n",
    "\n",
    "    model = tf.keras.models.Model(inputs=content, outputs=output)\n",
    "    model.compile(loss='categorical_crossentropy', \n",
    "                  optimizer='adam', \n",
    "                  metrics=['accuracy'])\n",
    "    return model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "model = build_model(maxlen, embedding_matrix,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model\"\n",
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_1 (InputLayer)            [(None, 100)]        0                                            \n",
      "__________________________________________________________________________________________________\n",
      "sentence_cuted (Embedding)      (None, 100, 200)     32267800    input_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "spatial_dropout1d (SpatialDropo (None, 100, 200)     0           sentence_cuted[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "bidirectional (Bidirectional)   (None, 100, 400)     482400      spatial_dropout1d[0][0]          \n",
      "__________________________________________________________________________________________________\n",
      "global_average_pooling1d (Globa (None, 400)          0           bidirectional[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "global_max_pooling1d (GlobalMax (None, 400)          0           bidirectional[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "concatenate (Concatenate)       (None, 800)          0           global_average_pooling1d[0][0]   \n",
      "                                                                 global_max_pooling1d[0][0]       \n",
      "__________________________________________________________________________________________________\n",
      "dense (Dense)                   (None, 1000)         801000      concatenate[0][0]                \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization (BatchNorma (None, 1000)         4000        dense[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "activation (Activation)         (None, 1000)         0           batch_normalization[0][0]        \n",
      "__________________________________________________________________________________________________\n",
      "dropout (Dropout)               (None, 1000)         0           activation[0][0]                 \n",
      "__________________________________________________________________________________________________\n",
      "dense_1 (Dense)                 (None, 500)          500500      dropout[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_1 (BatchNor (None, 500)          2000        dense_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "activation_1 (Activation)       (None, 500)          0           batch_normalization_1[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "dense_2 (Dense)                 (None, 3)            1503        activation_1[0][0]               \n",
      "==================================================================================================\n",
      "Total params: 34,059,203\n",
      "Trainable params: 1,788,403\n",
      "Non-trainable params: 32,270,800\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./checkpoints_predict\n",
      "./checkpoints_predict\\ckpt_{epoch}\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras.callbacks import EarlyStopping,ReduceLROnPlateau,ModelCheckpoint,TensorBoard\n",
    "# 检查点保存至的目录\n",
    "checkpoint_dir = './checkpoints_predict'\n",
    "\n",
    "# 检查点的文件名\n",
    "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_{epoch}\")\n",
    "print(checkpoint_dir)\n",
    "print(checkpoint_prefix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((89831, 100), (9982, 100), (89831,), (9982,))"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape,X_val.shape,y_train.shape,y_val.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "pycharm": {
     "is_executing": false
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train for 1404 steps, validate for 156 steps\n",
      "Epoch 1/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7132 - accuracy: 0.8984\n",
      "Epoch 00001: val_accuracy improved from -inf to 0.64606, saving model to ./checkpoints_predict\\ckpt_1\n",
      "1404/1404 [==============================] - 621s 442ms/step - loss: 3.7128 - accuracy: 0.8983 - val_loss: 3.6853 - val_accuracy: 0.6461\n",
      "Epoch 2/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7138 - accuracy: 0.7137\n",
      "Epoch 00002: val_accuracy improved from 0.64606 to 0.70276, saving model to ./checkpoints_predict\\ckpt_2\n",
      "1404/1404 [==============================] - 631s 450ms/step - loss: 3.7141 - accuracy: 0.7137 - val_loss: 3.7325 - val_accuracy: 0.7028\n",
      "Epoch 3/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7129 - accuracy: 0.7342\n",
      "Epoch 00003: val_accuracy improved from 0.70276 to 0.74664, saving model to ./checkpoints_predict\\ckpt_3\n",
      "1404/1404 [==============================] - 616s 439ms/step - loss: 3.7130 - accuracy: 0.7342 - val_loss: 3.6630 - val_accuracy: 0.7466\n",
      "Epoch 4/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7128 - accuracy: 0.7410\n",
      "Epoch 00004: val_accuracy did not improve from 0.74664\n",
      "1404/1404 [==============================] - 627s 447ms/step - loss: 3.7126 - accuracy: 0.7410 - val_loss: 3.6426 - val_accuracy: 0.7336\n",
      "Epoch 5/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7111 - accuracy: 0.7445\n",
      "Epoch 00005: val_accuracy did not improve from 0.74664\n",
      "1404/1404 [==============================] - 619s 441ms/step - loss: 3.7111 - accuracy: 0.7445 - val_loss: 3.6819 - val_accuracy: 0.7303\n",
      "Epoch 6/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7105 - accuracy: 0.7482\n",
      "Epoch 00006: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.\n",
      "\n",
      "Epoch 00006: val_accuracy did not improve from 0.74664\n",
      "1404/1404 [==============================] - 664s 473ms/step - loss: 3.7105 - accuracy: 0.7482 - val_loss: 3.7516 - val_accuracy: 0.7180\n",
      "Epoch 7/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7097 - accuracy: 0.7476\n",
      "Epoch 00007: val_accuracy improved from 0.74664 to 0.75205, saving model to ./checkpoints_predict\\ckpt_7\n",
      "1404/1404 [==============================] - 657s 468ms/step - loss: 3.7100 - accuracy: 0.7476 - val_loss: 3.6834 - val_accuracy: 0.7521\n",
      "Epoch 8/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7092 - accuracy: 0.7497\n",
      "Epoch 00008: val_accuracy did not improve from 0.75205\n",
      "1404/1404 [==============================] - 635s 453ms/step - loss: 3.7089 - accuracy: 0.7497 - val_loss: 3.6735 - val_accuracy: 0.7385\n",
      "Epoch 9/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7076 - accuracy: 0.7484\n",
      "Epoch 00009: val_accuracy did not improve from 0.75205\n",
      "1404/1404 [==============================] - 628s 447ms/step - loss: 3.7075 - accuracy: 0.7484 - val_loss: 3.6804 - val_accuracy: 0.7508\n",
      "Epoch 10/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7064 - accuracy: 0.7473\n",
      "Epoch 00010: val_accuracy improved from 0.75205 to 0.75937, saving model to ./checkpoints_predict\\ckpt_10\n",
      "1404/1404 [==============================] - 654s 466ms/step - loss: 3.7060 - accuracy: 0.7474 - val_loss: 3.6843 - val_accuracy: 0.7594\n",
      "Epoch 11/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7041 - accuracy: 0.7477\n",
      "Epoch 00011: val_accuracy did not improve from 0.75937\n",
      "1404/1404 [==============================] - 625s 445ms/step - loss: 3.7045 - accuracy: 0.7477 - val_loss: 3.7289 - val_accuracy: 0.7444\n",
      "Epoch 12/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7021 - accuracy: 0.7472\n",
      "Epoch 00012: val_accuracy did not improve from 0.75937\n",
      "1404/1404 [==============================] - 620s 442ms/step - loss: 3.7022 - accuracy: 0.7472 - val_loss: 3.7124 - val_accuracy: 0.7316\n",
      "Epoch 13/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.7009 - accuracy: 0.7502\n",
      "Epoch 00013: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.\n",
      "\n",
      "Epoch 00013: val_accuracy did not improve from 0.75937\n",
      "1404/1404 [==============================] - 666s 474ms/step - loss: 3.7009 - accuracy: 0.7502 - val_loss: 3.6875 - val_accuracy: 0.7405\n",
      "Epoch 14/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.6995 - accuracy: 0.7481\n",
      "Epoch 00014: val_accuracy did not improve from 0.75937\n",
      "1404/1404 [==============================] - 808s 575ms/step - loss: 3.6993 - accuracy: 0.7482 - val_loss: 3.6755 - val_accuracy: 0.7419\n",
      "Epoch 15/15\n",
      "1403/1404 [============================>.] - ETA: 0s - loss: 3.6978 - accuracy: 0.7501\n",
      "Epoch 00015: val_accuracy did not improve from 0.75937\n",
      "1404/1404 [==============================] - 654s 466ms/step - loss: 3.6980 - accuracy: 0.7501 - val_loss: 3.6347 - val_accuracy: 0.7346\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2e055869148>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "early_stopping = EarlyStopping(monitor='val_accuracy', patience=6)\n",
    "plateau = ReduceLROnPlateau(monitor=\"val_accuracy\", \n",
    "                            verbose=1, \n",
    "                            mode='max', \n",
    "                            factor=0.5, \n",
    "                            patience=3)\n",
    "checkpoint = ModelCheckpoint(checkpoint_prefix, \n",
    "                             monitor='val_accuracy',\n",
    "                             verbose=1, \n",
    "                             save_best_only=True,\n",
    "                             mode='max',\n",
    "                             save_weights_only=True)\n",
    "\n",
    "model.fit(train_ds,\n",
    "          epochs=15,\n",
    "          validation_data=val_ds,\n",
    "          callbacks=[early_stopping, plateau, checkpoint],\n",
    "          verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.training.tracking.util.CheckpointLoadStatus at 0x2e00feb56c8>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "valid_prob = model.predict(val_ds)\n",
    "valid_pred = np.argmax(valid_prob,axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, ..., 2, 2, 1])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "valid's macro_f1: 0.05142776062246388\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "# val_true = np.argmax(y_val,axis=1)\n",
    "\n",
    "from sklearn.metrics import f1_score\n",
    "print (\"valid's macro_f1: %s\" % f1_score(y_val,valid_pred,average='macro'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "df_sub = df_test[['微博id']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>微博id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4456068992182160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4456424178427250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4456797466940200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4456791021108920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4457086404997440</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               微博id\n",
       "0  4456068992182160\n",
       "1  4456424178427250\n",
       "2  4456797466940200\n",
       "3  4456791021108920\n",
       "4  4457086404997440"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sub.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "test_ds = tf.data.Dataset.from_tensor_slices((test_,np.zeros((test_.shape[0],3)))).batch(128)\n",
    "test_prob = model.predict(test_ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "test_preb = np.argmax(test_prob,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 2, ..., 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_preb\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\tempbeforenewdisk\\anaconda3\\envs\\jupyter\\lib\\site-packages\\ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "df_sub['y'] = test_preb-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>微博id</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4456068992182160</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4456424178427250</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4456797466940200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4456791021108920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4457086404997440</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               微博id  y\n",
       "0  4456068992182160 -1\n",
       "1  4456424178427250 -1\n",
       "2  4456797466940200  1\n",
       "3  4456791021108920  1\n",
       "4  4457086404997440  1"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sub.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "df_sub.columns=['id','y']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4456068992182160</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4456424178427250</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4456797466940200</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4456791021108920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4457086404997440</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 id  y\n",
       "0  4456068992182160 -1\n",
       "1  4456424178427250 -1\n",
       "2  4456797466940200  1\n",
       "3  4456791021108920  1\n",
       "4  4457086404997440  1"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_sub.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "df_sub.to_csv('test_sub.csv',index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}